import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:wan_android/pages/page_found_son.dart';

///发现页面
class PageFound extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return PageFoundState();
  }
}

class PageFoundState extends State<PageFound> {
  List<FindEntity> list = [];

  var totalData;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _requestJson();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return ListView.builder(
      itemBuilder: (BuildContext context, int dex) {
        return _getFoundItemUi(list[dex], context, totalData['data'][dex]);
      },
      itemCount: list.length,
    );
  }

  void _requestJson() async {
    Dio dio = new Dio();
    await dio.get(treeApi).then((response) {
      print('获取到数据');
      print(response.data);
      for (var value in response.data['data']) {
        //单个json对象
        String itemTitle;
        StringBuffer stringBuffer = new StringBuffer();
        itemTitle = value['name'];
        for (var value1 in value['children']) {
          //描述json对象
          stringBuffer.write(value1['name'] + "  ");
        }
        list.add(
            FindEntity(itemTitle: itemTitle, des: stringBuffer.toString()));
      }
      print(list);
      setState(() {
        totalData = response.data;
      });
    }).catchError((error) {
      print(error);
    }).whenComplete(() {
      print('found: 请求数据完成');
    });
  }
}

class FindEntity {
  int id;

  ///文章标题
  String itemTitle;

  /// 描述
  String des;
  String link;

  FindEntity({this.id, this.itemTitle, this.des, this.link});
}

Widget _getFoundItemUi(FindEntity entity, context, totalData) {
  if (entity == null) {
    return Center(
      child: Text('请稍等..'),
    );
  }
  return Container(
    child: GestureDetector(
      child: Card(
        child: Row(
          children: <Widget>[
            Expanded(
              child: Column(
                children: <Widget>[
                  Padding(
                    child: Align(
                      child: Text(
                        entity.itemTitle,
                        style: TextStyle(fontSize: 16.0),
                      ),
                      alignment: AlignmentDirectional.centerStart,
                    ),
                    padding: EdgeInsets.only(top: 5.0, left: 5.0),
                  ),
                  Expanded(
                    child: Align(
                      child: Padding(
                        padding: EdgeInsets.only(left: 5.0),
                        child: Text(
                          entity.des,
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(fontSize: 14.0),
                        ),
                      ),
                      alignment: AlignmentDirectional.centerStart,
                    ),
                  ),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(right: 5.0),
              child: Icon(Icons.chevron_right),
            ),
          ],
        ),
        elevation: 5.0,
        margin: EdgeInsets.all(5.0),
      ),
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return PageSon(totalData: totalData);
        }));
      },
    ),
    height: 100.0,
  );
}

String treeApi = 'http://www.wanandroid.com/tree/json';
